package com.shuaiche.yeguan.views.brandselectbar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.ui.select.SCCarBrandRecommendAdapter;
import com.shuaiche.yeguan.util.ResourceUtils;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by baoyinbo .
 * Date: 17/08/28
 */

public class BrandAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private int ITEM_RECOMMEND = 1;     //推荐、历史等
    private int ITEM_CONTENT = 2;

    private Context mContext;
    private List<ItemBean> mDatas;
    private LayoutInflater mInflater;
    private boolean isAdd = false;

    public BrandAdapter(Context mContext, List<ItemBean> mDatas, boolean isAdd) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(mContext);
        this.isAdd = isAdd;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        if (viewType == ITEM_RECOMMEND) {
            view = mInflater.inflate(R.layout.sc_item_brand_recommend_list, parent, false);
            holder = new ViewHolderRecommend(view);
        } else {
            view = mInflater.inflate(R.layout.sc_item_brand_first_list, parent, false);
            holder = new ViewHolderList(view);

        }
        return holder;

    }

    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
        final ItemBean itemBean = mDatas.get(position);
        if (holder instanceof ViewHolderList) {
            ((ViewHolderList)holder).tvCity.setText(itemBean.getName());

            if (itemBean.isSelect()) {
                ((ViewHolderList)holder).tvCity.setTextColor(ResourceUtils.getColor(mContext, R.color.text_red));
            } else {
                ((ViewHolderList)holder).tvCity.setTextColor(ResourceUtils.getColor(mContext, R.color.text_black));
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callListener.itemClick(position);
                }
            });
        } else {
            GridLayoutManager recommendManager = new GridLayoutManager(mContext, 5);
            ((ViewHolderRecommend)holder).rvRecommend.setLayoutManager(recommendManager);
            SCCarBrandRecommendAdapter recommendAdapter =
                    new SCCarBrandRecommendAdapter(mContext, itemBean.getRecommendBrands());
            ((ViewHolderRecommend)holder).rvRecommend.setAdapter(recommendAdapter);
            recommendAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                    callListener.itemRecommendClick(i);
                }
            });
            if (isAdd) {
                ((ViewHolderRecommend) holder).tvRecommendTitle.setText("常用品牌");
            } else {
                ((ViewHolderRecommend) holder).tvRecommendTitle.setText("推荐品牌");
            }

        }
    }



    @Override
    public int getItemViewType(int position) {
        if (mDatas.get(position).getRecommendBrands() != null) {
            return ITEM_RECOMMEND;
        } else {
            return ITEM_CONTENT;
        }
//        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    /**
     * 列表
     */
    public static class ViewHolderList extends RecyclerView.ViewHolder {
        TextView tvCity;

        public ViewHolderList(View itemView) {
            super(itemView);
            tvCity = (TextView) itemView.findViewById(R.id.tvCity);
        }
    }

    /**
     * 推荐
     */
    public static class ViewHolderRecommend extends RecyclerView.ViewHolder {
        RecyclerView rvRecommend;
        TextView tvRecommendTitle;
        public ViewHolderRecommend(View itemView) {
            super(itemView);
            rvRecommend = (RecyclerView) itemView.findViewById(R.id.rvRecommend);
            tvRecommendTitle = (TextView) itemView.findViewById(R.id.tvRecommendTitle);
        }
    }



    public interface CallbackListener{
        void itemClick(int position);

        /**
         * 推荐品牌点击
         * @param position
         */
        void itemRecommendClick(int position);
    }

    /**回调 拨打电话 / 隐藏 监听*/
    public CallbackListener callListener;

    public void setCallbackListener (CallbackListener callListener) {
        this.callListener = callListener;
    }
}
